package com.hxxy.wyp.petsLover.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.hxxy.wyp.petsLover.common.entities.vo.req.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000122613958";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCG9eTvn2bMlTpo4nN40If9PiBiY8KnzEWDSbBnwMJi15LRuM94faXulDKDb543Q7XTj2THdx5BWS+Tglypt83n/eTS3xz2KCZhk/DfW3Nc4neGRgdpltlY2p/f6rSGR21y2rV6HWoCJEii3sDOSnXUAwappWp11FYCAVKDczKwhIVl19/9a6CffO+nK8jc39GhD5K2JR0aly3rvtLH0qOb9R8n848ou9Q65H+QgwgkFfDH/lmL95r7M5x3ZkiBa6HdLgy6NUzXVRriOX9y//+kuMG41359/b6jm3EOiVHini04/KOpSsbACa3vzPcvp2qKUmGb+3FGDRtRwO4UwAGbAgMBAAECggEAZnXqcA2JgwpMRPKfDfrrM1TmSyxHzRKlN45LUgmiyl21hmkTgfuKbT2vt9+Ws4hBU7OKpHCH1L78ArY22VbFCTrVbU6b8zn6IZyqgzgvtjXs3lDuMbinm/Cm7d2HXmZ97nOi0WnhYGzhhF0u7a7UE7iUZ1lBIOGqxmzwkwFzW1SrWazIVkQ/YcMaB9jlar/HLgaze53AMU3OPIw8glVvOPWoT3wbPk8TaQvBo0HoZ6M/w6063pxPsBOb+BPDvsviWnYc7jPSf0w8F7n+uZLKN+KupHJg2iJLBPpVWYcS3Kl3C3mOKBmfS1zdWVpcy8UKGe1zfEe4R6rOR7tUWqarWQKBgQD2YrKDPXLWPNMC3zFnqzneft/l6c+oDSzUpR902/LijMoxWOodS3OMm6VIFk5ZOJ0uNxxy6FE1+wEDtB7OPMIsNcQmKky44RqdgRh6q5gyXPylg/gkdc78HZ+yjg95JrpHj4DscugEMEmyS4V4JEbcNLWRX26sTLaf90dXzn/Y5wKBgQCMOhnsp/NznySrj4u1vz0CJKQfuvO8Ny3ajChaSoPuCWKVU2fVyEHivpH0De/8dJFSOcy8YeifRgBOpHs5Uo0AKpqXyD1TiGplEtlqY4d0z1pRTJ9mexHAF+ubaYgCZrb3KpbGt3xEl2UYI9H9OZT/JLMHQCFAkKsYWgTfYgD3LQKBgCQrhxNXMQWcdknEpEcYzu0H6Syib3a3QG6TMxP/uh9iFci6ydu1GOh6bQAxLh1Q/1oqgwWnb0yiZZq6lY8nQkYlOn9z1EGL4lFfA0325Of32KsfvYz0soc2fhxDWKgrdEtPGNkdYUxEHn2LMrPZd4GaR1FpFiE7PdAsCBAu43vfAoGAO6FVduKwz0EkmeQP+Dibm/ay6F9a2HXqhN5HJqY7VhUdm/x80lc3VVHOxSbMH+H+DiZQHufPwTNGXCrrz6OAHEsBtHvNQGFpRRPv6qLqbkzizmG0AyNgl/xUD6Gf2c2ODqtItLZAHPb4YXySBonfB2S20OV/fnJGn7NgERIe8P0CgYEA1bKimbGP8mgdIJo7k6uvUB7AewjSWDsDPKR68Aj88Q40P3OJeOYHwL90A+km74cGxqUhQ9eqauoylTDOfRnAqWdve2z1yrsh0gdwy4CnyFjLHL9Lx5VBtg0++3dlcdLdRexIy6K4gI0NwL6SIxoYyFgwpHDdmnmvMdyZNvvvf1I=";    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAh5uyZMyUWhJseA7QsqrAhVqjhfaoiheIkVvQE+9H1tVJpOCGYBUYP5oRNNlXTm3WORgrFh9GSg30mDDCvz+NkXT8jT1iM/dnNpRl1Gnt2iznsYZZnsYJmj8heIYDOTUpmVaZlmdgpVFvqaouH1lo+aMZnZ6c+QBa5VodIQBabqBzuy0OXIbjResyX1zL/I2ddc9tqzkXb/LD8tGsSQGfv0nVHSlUkyA8e/kR0/qJ3AyQvvot68TrWQHsdsiBBjqh+6AYkZ5CM2bv4okfNH7TvQRH6vUo9Bkj5ybhuyrfAQOI0cwAwaCSqDxKpTILQQOsJtm1F84RwvWQL6gu2fjcZwIDAQAB";

    private  String notify_url = "http://bapoint.top:5173/#/UserCenter";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://bapoint.top:5173/#/UserCenter";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 自动关单时间
    private String timeout = "15m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        // 30分钟内不付款就会自动关单
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        return result;
    }
}